package net.xinqushi.design.proxy;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

public class Test {

    public static void main(String[] args) throws InvocationTargetException, IllegalAccessException, NoSuchMethodException {
//        ProxyFactory factory = new DefaultProxyFactory(Target.getInstance());
//        Object proxyInstance = factory.getProxyInstance();
//        Method method = proxyInstance.getClass().getMethod("run");
//        method.invoke(proxyInstance);

        ProxyFactory factory = new JdkProxyFactory(Target.getInstance());
        Object proxyInstance = factory.getProxyInstance();
        Method method = proxyInstance.getClass().getMethod("run");
        method.invoke(proxyInstance);

    }

}

class Target implements Delegate{

    private static Target target;
    public void run (){
        System.out.println("run");
    }

    public Target(){};
    static Target getInstance(){
        return null == target ? (target = new Target()) : target;
    }
}

interface Delegate {
    void run ();
}
